package com.assets.Receive;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.assets.newNHF.util.LogUtils;
import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.assets.Adapter.Receive_da;
import com.assets.R;
import com.assets.CanniBalize.Shift_site;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/16.
 */

public class add_category extends Activity implements OnDismissListener {
    private ListView add_lv;
    private LinearLayout property, position;//资产名称，使用位置
    private TextView property_tv, position_tv, user;
    private EditText add_number;
    private Button upload, add;
    List<Map<String, Object>> lv_list = new ArrayList<Map<String, Object>>();
    private String url, site_id = "", property_num = "";
    private ImageView fh;
    private TextView title_text;
    private ArrayList<String> stringArrayList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_consumable);
        //url = "http://192.168.1.159:8087/api/getAllPut";
        inview();
        String company_id = getIntent().getStringExtra("company_id");
        user.setOnClickListener(v -> {
            Intent intent = new Intent(add_category.this, add_all_user.class);
            startActivityForResult(intent, 4);
        });
        property.setOnClickListener(v -> {
            /*Intent intent = new Intent(add_category.this, Receive_property.class);
            intent.putExtra("company_id", company_id);
            intent.putExtra("stringList", stringArrayList);
            startActivityForResult(intent, 3);*/
        });
        position.setOnClickListener(v -> {
            Intent intent = new Intent(add_category.this, Shift_site.class);
            startActivityForResult(intent, 2);
        });

        upload.setOnClickListener(v -> {
            if (lv_list.size() > 0) {
                JSONArray array = new JSONArray();
                for (int i = 0; i < lv_list.size(); i++) {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("name", lv_list.get(i).get("coding_name"));
                        object.put("num", lv_list.get(i).get("receivesDept_name"));
                        object.put("user", lv_list.get(i).get("receivesUser_name"));
                        object.put("place", lv_list.get(i).get("reapplyDate_name"));
                        array.put(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("list", array.toString());
                add_category.this.setResult(6, intent);
                add_category.this.finish();//把item的内容返回给之前的activity
            } else {
                Toast.makeText(add_category.this, "请先添加资产", Toast.LENGTH_LONG).show();
            }
        });
        add_lv.setOnItemClickListener((parent, view, i, id) -> new AlertView("提示", "是否删除", "取消", new String[]{"确定"}, null, add_category.this, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                //取消：position=-1,确定 position=0
                if (position == 0) {
                    lv_list.remove(i);
                    Receive_da receive_da = new Receive_da(add_category.this, lv_list);
                    add_lv.setAdapter(receive_da);
                    stringArrayList.remove(i);
                }

            }
        }).setCancelable(true).setOnDismissListener(add_category.this).show());
        add.setOnClickListener(v -> {
            /*if (property_num.equals("")) {
                Toast.makeText(add_category.this, "请选择选择资产类型后重试！", Toast.LENGTH_LONG).show();
                return;
            }
            int i = Integer.parseInt(property_num);*/
            int j = Integer.parseInt(add_number.getText().toString());
            if (property_tv.getText().toString().equals("") || user.getText().toString().equals("") || site_id.equals("") || add_number.getText().toString().equals("")) {
                Toast.makeText(add_category.this, "请完善数据后重试！", Toast.LENGTH_LONG).show();
                return;
            }
            /*if (j > i) {
                Toast.makeText(add_category.this, "数量不能大于库存数量！此时库存数量为：" + property_num, Toast.LENGTH_LONG).show();
                return;
            }*/
            if (j == 0) {
                Toast.makeText(add_category.this, "数量不能为0", Toast.LENGTH_LONG).show();
                return;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("coding_name", property_tv.getText().toString().trim());
            map.put("receivesDept_name", add_number.getText().toString());
            map.put("receivesUser_name", user.getText().toString());
            map.put("reapplyDate_name", position_tv.getText().toString());
            stringArrayList.add(property_tv.getText().toString().trim());
            lv_list.add(map);
            Receive_da receive_da = new Receive_da(add_category.this, lv_list);
            add_lv.setAdapter(receive_da);
            property_tv.setText("");
            position_tv.setText("");
            site_id = "";
            property_num = "";
            add_number.setText("");
            user.setText("");
        });
    }

    private void inview() {
        user = (TextView) findViewById(R.id.user);
        position_tv = (TextView) findViewById(R.id.position_tv);
        position = (LinearLayout) findViewById(R.id.position);
        add_number = (EditText) findViewById(R.id.num);
        property_tv = (TextView) findViewById(R.id.property_tv);
        property = (LinearLayout) findViewById(R.id.property);
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("添加资产");
        fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        upload = (Button) findViewById(R.id.upload);
        add_lv = (ListView) findViewById(R.id.add_lv);
        add = (Button) findViewById(R.id.add);
    }

    @Override
    public void onDismiss(Object o) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == 3) {
            position_tv.setText(data.getExtras().getString("site_name"));
            site_id = data.getExtras().getString("site_id");
            return;
        }
        if (requestCode == 3 && resultCode == 4) {
            String name = data.getExtras().getString("name");
            property_tv.setText(name);
            property_num = data.getExtras().getString("num");
            add_number.setText(property_num);
            return;
        }
        if (requestCode == 4 && resultCode == 3) {
            String name = data.getExtras().getString("recipient_name");
            user.setText(name);
            LogUtils.d(name);
            return;
        }
    }
}
